﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Configuration {

    public class RoleModel : ConfigurationItemModel {
                
        #region Private Properties

        private Security.Role role = null;

        private Boolean forEnterprise = false;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { RoleId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return role; } }


        public Int64 RoleId {

            set {

                role = Application.Security.Role (value);

                if (role == null) {

                    role = new Security.Role (Application);

                }

            }

        }

        public Security.Role Role { get { return role; } set { role = value; } }

        public Boolean ForEnteprise { get { return forEnterprise; } set { forEnterprise = value; } }


        public System.Web.Mvc.SelectList EnvironmentSelectList {

            get {

                List<Enterprise.Environment> items;

                items = (from currentEnvironment in Application.Enterprise.Environments()

                         orderby currentEnvironment.Name

                         select currentEnvironment).ToList ();

                return new System.Web.Mvc.SelectList (items, "Id", "Name");

            }

        }

        public List<Security.RolePermission> RolePermissionsAvailable {

            get {

                List<Security.RolePermission> rolePermissions = new List<Security.RolePermission> ();

                List<Security.Permission> permissionsAvailable;

                if (forEnterprise) { permissionsAvailable = Application.Security.PermissionsEnterprise (true); }

                else { permissionsAvailable = Application.Security.PermissionsEnvironment (true); }

                foreach (Security.Permission currentPermission in permissionsAvailable) {

                    Security.RolePermission rolePermission = new Security.RolePermission (Application);

                    rolePermission.RoleId = role.Id;

                    rolePermission.PermissionId = currentPermission.Id;

                    rolePermission.Name = currentPermission.Name;

                    rolePermission.Description = currentPermission.Description;


                    Security.RolePermission sourceRolePermission = Role.Permission (currentPermission.Id);

                    if (sourceRolePermission != null) {

                        rolePermission.Granted = sourceRolePermission.Granted;

                        rolePermission.Denied = sourceRolePermission.Denied;

                    }


                    rolePermissions.Add (rolePermission);

                }

                return rolePermissions;

            }

        }

        public System.Web.Mvc.SelectList SecurityAuthoritySelectList {

            get {

                List<Security.SecurityAuthority> items;

                items = (from currentSecurityAuthority in Application.Security.SecurityAuthorities ()

                         where (currentSecurityAuthority.Enabled = true)

                         orderby currentSecurityAuthority.Name

                         select currentSecurityAuthority).ToList ();

                return new System.Web.Mvc.SelectList (items, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public RoleModel (Int64 forRoleId) {

            RoleId = forRoleId;

            return;

        }

        public RoleModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);

            // BASE RELOADS THE MAIN ROLE OBJECT 


            Role.EnvironmentId = Data.Readers.FormReader.Int64FromForm (form, "Role.EnvironmentId"); 


            // ROLE PERMISSIONS DESERIALIZE JSON 

            Role.Permissions.Clear ();

            String jsonRolePermissions = Data.Readers.FormReader.StringFromForm (form, "Role.Permissions");

            Role.Permissions = (List<Security.RolePermission>)new System.Web.Script.Serialization.JavaScriptSerializer ().Deserialize (jsonRolePermissions, Role.Permissions.GetType ());


            // MEMBERSHIP DESERIALIZE JSON

            Role.Membership.Clear ();

            String jsonRoleMembership = Data.Readers.FormReader.StringFromForm (form, "Role.Membership");

            Role.Membership = (List<Security.RoleMembership>)new System.Web.Script.Serialization.JavaScriptSerializer ().Deserialize (jsonRoleMembership, Role.Membership.GetType ());

            return;

        }

        #endregion 

    }

}